package demo.mianshi.thread;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.LockSupport;

public class _ABC2 {
    static volatile int i = 0;
    static volatile int k = 0;

    public static void main(String[] args) {
        //开启 3 个线程，这 3 个线程的 ID 分别为 A、B、C，3 个线程交替打印 1-100 的整数
//        m1();
        m2();
    }

    static int t = 3;
    static int m = 100;
    static int c = 0;
    static int idx = 0;

    private static void m2() {
        List<Thread> tl = new ArrayList<>();
        for (int j = 0; j < t; j++) {
            Thread thread = new Thread(() -> {
                while (true) {
                    LockSupport.park();
                    if (c < m) {
                        c++;
                        System.out.println(c);
                    } else {
                        break;
                    }
                    // 唤醒下一个
                    idx++;
                    LockSupport.unpark(tl.get(idx % t));
                }
                // 唤醒全部
                tl.forEach(LockSupport::unpark);
            }, "" + j);
            tl.add(thread);
        }

        for (Thread thread : tl) {
            thread.start();
        }
        LockSupport.unpark(tl.get(0));
    }

    private static void m1() {
        Object lock = new Object();
        int c = 100 - 3;
        new Thread(() -> {
            synchronized (lock) {
                while (k <= c) {
                    while (i != 0) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    k++;
                    System.out.print(k + ",");
                    i = 1;
                    lock.notifyAll();
                }
            }
        }).start();
        new Thread(() -> {
            synchronized (lock) {
                while (k <= c) {
                    while (i != 1) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    k++;
                    System.out.print(k + ",");
                    i = 2;
                    lock.notifyAll();
                }
            }
        }).start();
        new Thread(() -> {
            synchronized (lock) {
                while (k <= c) {
                    while (i != 2) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    k++;
                    System.out.print(k + ",");
                    i = 0;
                    lock.notifyAll();
                }
            }
        }).start();
    }
}
